#include "b_bishop.h"
#include "board.h"
#include "gameengine.h"
#include <iostream>

using namespace std;

Bishop::Bishop() {
}

Bishop::Bishop(int r, int c , Color color) {
	row = r;
	col = c;
	pColor = color;
	setImage();
}


Bishop::~Bishop() {

}

void Bishop::setImage() {
	if(pColor==white) {
		image = W_BISHOP;
	} else {
		image = B_BISHOP;
	}
}


void Bishop::GetAllPossible(std::vector <Cell>  & moves) {
	bool posposStop = false;
	bool negposStop = false;
	for( int i = 1 ; i <= 7 ; i++) {
		if(MyFunctions::isInRange(row+i,col+i) && !posposStop) {
			moves.push_back(Cell(row+i,col+i));
			if(myBoard->hasEnemy(pColor,row+i,col+i)) {
				posposStop = true;
			}
			if(myBoard->hasTeamate(pColor,row+i,col+i)) {
				moves.pop_back();
				posposStop = true;
			}
		}

		if(MyFunctions::isInRange(row-i,col+i) && !negposStop) {
			moves.push_back(Cell(row-i,col+i));
			if(myBoard->hasEnemy(pColor,row-i,col+i)) {
				negposStop = true;
			}
			if(myBoard->hasTeamate(pColor,row-i,col+i)) {
				moves.pop_back();
				negposStop = true;
			}
		}
	}
	SearchMoreMoves(moves);

}

void Bishop::SearchMoreMoves(std::vector<Cell> & moves) {

	bool negnegStop = false;
	bool posnegStop = false;
	for( int i = 1 ; i <= 7 ; i++) {
		if(MyFunctions::isInRange(row-i,col-i) && !negnegStop) {
			moves.push_back(Cell(row-i,col-i));
			if(myBoard->hasEnemy(pColor,row-i,col-i)) {
				negnegStop = true;
			}
			if(myBoard->hasTeamate(pColor,row-i,col-i)) {
				moves.pop_back();
				negnegStop = true;
			}
		}

		if(MyFunctions::isInRange(row+i,col-i) && !posnegStop) {
			moves.push_back(Cell(row+i,col-i));
			if(myBoard->hasEnemy(pColor,row+i,col-i)) {
				posnegStop = true;
			}
			if(myBoard->hasTeamate(pColor,row+i,col-i)) {
				moves.pop_back();
				posnegStop = true;
			}
		}
	}
}

bool Bishop::isPiece() {
	return true;
}


void Bishop::setCell(int r, int c) {
	row = r;
	col = c;
}

Color Bishop::GetColor() {
	return pColor;
}

int Bishop::getValue() {
    return 3;
}

ImageName Bishop::GetImage() {
	return image;
}


bool Bishop::Test(std::ostream &os) {
	bool success = true;
	if (!Bishop::TestMoves(std::cout)) success = false;
	if (!Bishop::TestCheck(std::cout)) success = false;
	return success;
}

bool Bishop::TestMoves(std::ostream &os) {

	bool success = true;

	GameEngine testGame;
	testGame.gameStarted = true;
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(7,1,white));
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(5,1,white));
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(0,0,white));
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(1,4,white));
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(2,1,white));
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(5,6,white));

	testGame.myBoard.piecesOnBoard.push_back(new Pawn(4,3,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(4,5,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(4,7,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(6,5,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(6,7,black));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(6,0,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(6,1,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(6,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(4,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(0,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(2,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(0,5,white));
	testGame.myBoard.piecesOnBoard.push_back(new Pawn(2,5,white));

	testGame.turn = white;
	ChessPiece * p1 = testGame.myBoard.piecesOnBoard[1-1];
	ChessPiece * p2 = testGame.myBoard.piecesOnBoard[2-1];
	ChessPiece * p3 = testGame.myBoard.piecesOnBoard[3-1];
	ChessPiece * p4 = testGame.myBoard.piecesOnBoard[4-1];
	ChessPiece * p5 = testGame.myBoard.piecesOnBoard[5-1];
	ChessPiece * p6 = testGame.myBoard.piecesOnBoard[6-1];

	p1->setBoard(&testGame.myBoard);
	p2->setBoard(&testGame.myBoard);
	p3->setBoard(&testGame.myBoard);
	p4->setBoard(&testGame.myBoard);
	p5->setBoard(&testGame.myBoard);
	p6->setBoard(&testGame.myBoard);

	std::vector<Cell> moves1;
	std::vector<Cell> moves2;
	std::vector<Cell> moves3;
	std::vector<Cell> moves4;
	std::vector<Cell> moves5;
	std::vector<Cell> moves6;

	p1->GetAllPossible(moves1);
	p2->GetAllPossible(moves2);
	p3->GetAllPossible(moves3);
	p4->GetAllPossible(moves4);
	p5->GetAllPossible(moves5);
	p6->GetAllPossible(moves6);

	std::vector<Cell>answer1;
	std::vector<Cell>answer2;
	std::vector<Cell>answer3;
	std::vector<Cell>answer4;
	std::vector<Cell>answer5;
	std::vector<Cell>answer6;

	answer2.push_back(Cell(4,0));

	answer3.push_back(Cell(1,1));
	answer3.push_back(Cell(2,2));
	answer3.push_back(Cell(3,3));
	answer3.push_back(Cell(4,4));
	answer3.push_back(Cell(5,5));
	answer3.push_back(Cell(6,6));
	answer3.push_back(Cell(7,7));

	answer5.push_back(Cell(1,0));
	answer5.push_back(Cell(1,2));
	answer5.push_back(Cell(3,0));
	answer5.push_back(Cell(3,2));
	answer5.push_back(Cell(4,3));

	answer6.push_back(Cell(4,5));
	answer6.push_back(Cell(4,7));
	answer6.push_back(Cell(6,5));
	answer6.push_back(Cell(6,7));


	TEST(Pawn::CheckCellVectors(moves1,answer1));
	TEST(Pawn::CheckCellVectors(moves2,answer2));
	TEST(Pawn::CheckCellVectors(moves3,answer3));
	TEST(Pawn::CheckCellVectors(moves4,answer4));
	TEST(Pawn::CheckCellVectors(moves5,answer5));
	TEST(Pawn::CheckCellVectors(moves6,answer6));



	return success;
}

bool Bishop::TestCheck(std::ostream &os) {

	bool success = true;
	GameEngine testGame;
	testGame.gameStarted = true;
	testGame.turn = white;

	testGame.myBoard.piecesOnBoard.push_back(new Bishop(5,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Bishop(5,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new King(6,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(3,0,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(2,3,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(4,4,black));

	ChessPiece * p1 = testGame.myBoard.piecesOnBoard[0];
	ChessPiece * p2 = testGame.myBoard.piecesOnBoard[1];


	std::vector<ChessPiece *>::iterator it;
	for ( it = testGame.myBoard.piecesOnBoard.begin() ;
	        it < testGame.myBoard.piecesOnBoard.end() ; it++) {
		ChessPiece * p = *it;
		p->setBoard(&testGame.myBoard);
	}


	std::vector<Cell> moves1;
	std::vector<Cell> moves2;

	testGame.FindMoves(moves1,p1);
	testGame.FindMoves(moves2,p2);

	std::vector<Cell> answer1;
	std::vector<Cell> answer2;

	answer1.push_back(Cell(3,0));
	answer1.push_back(Cell(4,1));

	TEST(Pawn::CheckCellVectors(moves1,answer1));
	TEST(Pawn::CheckCellVectors(moves2,answer2));

	return success;
}


/*
  TEST MOVES------------------------

  Capital = black
  lower = white
  p = pawn
  Q = queen
  b = bishop

    0 1 2 3 4 5 6 7
  0 b . . p . p . .
  1 . . . . b . . .
  2 . b . p . p . .
  3 . . . . . . . .
  4 . . p P . P . P
  5 . b . . . . b .
  6 p p p . . P . P
  7 . b . . . . . .

  TEST CHECK------------------------

  Capital = black
  lower = white
  b = bishop
  q = queen
  k = king

    0 1 2 3 4 5 6 7
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . Q . . . .
  3 Q . . . . . . .
  4 . . . . Q . . .
  5 . . b b . . . . TEST 1 | TEST 2
  6 . . . k . . . .
  7 . . . . . . . .

    0 1 2 3 4 5 6 7
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . . . . . .
  3 . . . . . . . .
  4 . . . . . . . .
  5 . . . . . . . .
  6 . . . . . . . .
  7 . . . . . . . .


  */
